package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.service.RoleService;
import com.qk.entity.Role;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    //角色列表分页查询
    @GetMapping
    public Result querylist(String name, String label,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("查询角色列表：{},状态：{},页码：{},每页信息数：{}", name, label, page, pageSize);
        PageResult<Role> rolePageResult = roleService.selectlist(name, label, page, pageSize);
        return Result.success(rolePageResult);
    }

    //删除角色
    @Log
    @Delete("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除角色：{}", id);
        roleService.delete(id);
        return Result.success();
    }

    //添加角色
    @Log
    @PostMapping
    public Result add(@RequestBody Role role) {
        log.info("添加角色：{}", role);
        roleService.add(role);
        return Result.success();
    }

    //根据id插叙角色，get方式请求
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询角色：{}", id);
        return Result.success(roleService.getById(id));
    }

    //修改角色
    @Log
    @PutMapping
    public Result update(@RequestBody Role role) {
        log.info("修改角色：{}", role);
        roleService.update(role);
        return Result.success();
    }

    //查询所有角色
    @GetMapping("/list")
    public Result queryAllList(Role role) {
        log.info("查询所有角色：{}",role);
        return Result.success(roleService.list());
    }
}
